package com.htbb.common.util.security;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

public class AESUtil {

	private static final String AES_Mode = "AES/ECB/PKCS5Padding";
	
	/**
	 * AES加密再进行base64编码
	 * 
	 * @param content
	 *            待加密的内容
	 * @param encryptKey
	 *            加密密钥
	 * @return 加密后的base64编码字符串
	 * @throws Exception
	 */
	public static String aesEncrypt(String content, String encryptKey, String encoding) throws Exception {
		return base64Encode(aesEncryptToBytes(content, encryptKey, encoding));
	}

	/**
	 * base64解码再进行AES解密
	 * 
	 * @param encryptStr
	 *            待解密的base64字符串
	 * @param decryptKey
	 *            解密密钥
	 * @return 解密后的string
	 * @throws Exception
	 */
	public static String aesDecrypt(String encryptStr, String decryptKey, String encoding) throws Exception {
		return aesDecryptByBytes(base64Decode(encryptStr), decryptKey, encoding);
	}
	
	public static SecretKeySpec getKey(String strKey) {
		try {
			if (strKey == null) {
				strKey = "";
			}
			return new SecretKeySpec(getKeyBytes(strKey), "AES");
		} catch (Exception e) {
			throw new RuntimeException("初始化密钥出现异常 ");
		}
	}
	
	public static byte[] getKeyBytes(String strKey) throws NoSuchAlgorithmException{
		KeyGenerator _generator = KeyGenerator.getInstance("AES");
		SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
		secureRandom.setSeed(strKey.getBytes());
		_generator.init(128, secureRandom);
		return _generator.generateKey().getEncoded();
	}

	/**
	 * AES加密
	 * 
	 * @param content
	 *            待加密的内容
	 * @param encryptKey
	 *            加密密钥
	 * @return 加密后的字节数组
	 * @throws Exception
	 */
	public static byte[] aesEncryptToBytes(String content, String encryptKey, String encoding) throws Exception {
		
		Cipher cipher = Cipher.getInstance(AES_Mode);
		cipher.init(Cipher.ENCRYPT_MODE, getKey(encryptKey));

		return cipher.doFinal(content.getBytes(encoding));
	}

	/**
	 * AES解密
	 * 
	 * @param encryptBytes
	 *            待解密的字节数组
	 * @param decryptKey
	 *            解密密钥
	 * @return 解密后的String
	 * @throws Exception
	 */
	public static String aesDecryptByBytes(byte[] encryptBytes, String decryptKey, String encoding) throws Exception {
		
		Cipher cipher = Cipher.getInstance(AES_Mode);
		cipher.init(Cipher.DECRYPT_MODE, getKey(decryptKey));
		byte[] decryptBytes = cipher.doFinal(encryptBytes);

		return new String(decryptBytes, encoding);
	}

	/**
	 * base64编码
	 * 
	 * @param bytes
	 *            待编码的字节数组
	 * @return 编码后的base64字符串
	 */
	public static String base64Encode(byte[] bytes) {
		return Base64.encodeBase64String(bytes);
	}

	/**
	 * base64解码
	 * 
	 * @param base64Code
	 *            待解码的base64字符串
	 * @return 解码后的字节数组
	 * @throws Exception
	 */
	public static byte[] base64Decode(String base64Str) {
		return Base64.decodeBase64(base64Str);
	}
	
	public static void main(String[] args) throws Exception {
		System.out.println(aesEncrypt("{adda:212121}", "454", "UTF-8"));
		System.out.println(aesDecrypt("5wmUGuSSoGkQ65K5KmgBHw==","454", "UTF-8"));
	}
}
